 <template>
	<view class="navbar">
		<view class="navbar-fixed">
			<!-- 状态栏 -->
			<view :style="{height: statusBarHeight + 'px'}"></view>
			<!-- 导航栏内容 -->
			<view class="navbar-content" :class="{search:isSearch}" :style="{height:navbarHeight+'px',width:windowWidth+'px'}" @click.stop="open">
				<view v-if="isSearch" class="navbar-content__search-icons" @click="back">
					<uni-icons type="back" size="22" color="#fff"></uni-icons>
				</view>
				<view v-if="!isSearch" class="navbar-search">
					<!-- 非搜索页显示 -->
					<view class="navbar-search-icon">
						<!-- <text class="iconfont icon-search"></text> -->
						<uni-icons type="search" size="16" color="#999"></uni-icons>
					</view>
					<view class="navbar-search-text">
						uniapp/、vue
					</view>
				</view>
				<view v-else class="navbar-search">
					<!-- 搜索页显示 -->
					<input type="text" class="navbar-search-text" @input="inputChange" v-model="val" placeholder="请输入您要搜索的内容"/>
				</view>
			</view>
		</view>
		<!-- 这是一个占位元素，因为tabbar固定了 -->
		<view :style="{height: statusBarHeight+navbarHeight+'px'}"></view>
	</view>
</template>

<script>
	import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue'
	export default {
		props:{
			value:{
				type:[String,Number],
				default:''
			},
			isSearch:{
				type:Boolean,
				default:false
			}
		},
		components:{
			uniIcons
		},
		data() {
			return {
				statusBarHeight: 20, //状态栏高度
				navbarHeight: 40, //导航栏高度
				windowWidth: 375,
				val:''
			};
		},
		watch:{
			value(newVal){
				this.val = newVal
			}
		},
		created() {
			/* 获取手机系统信息 */
			const info = uni.getSystemInfoSync()
			// 设置状态栏的高度 
			this.statusBarHeight = info.statusBarHeight
			// 设置屏幕宽度
			this.windowWidth = info.windowWidth
			// #ifndef H5 || APP-PLUS || MP-ALIPAY
				// 获取胶囊的位置
				const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
				// (胶囊底部高度 - 状态栏高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
				this.navbarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info.statusBarHeight)

				this.windowWidth = menuButtonInfo.left
			// #endif

		},
		methods:{
			back(){
				uni.switchTab({
					url:'/pages/tabbar/index/index'
				})
			},
			open(){
				if(this.isSearch) return
				uni.navigateTo({
					url:"/pages/home-search/home-search"
				})
			},
			inputChange(e){
				const {value} = e.detail
				this.$emit('input',value)
			}
		}

	}
</script>

<style lang="scss">
	@import "../../common/css/icon.css";
	.navbar {
		.navbar-fixed {
			width: 100%;
			background-color: $mk-base-color;

			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;

			.navbar-content {
				// height: 45px;
				padding: 0 15px;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;

				.navbar-search {
					height: 30px;
					width: 100%;
					padding: 0 10px;
					background-color: #fff;
					display: flex;
					align-items: center;
					border-radius: 30px;

					.navbar-search-icon {
						// width: 10px;
						// height: 10px;
						// border: 1px solid red;
						margin-right: 10px;
					}

					.navbar-search-text {
						width: 100%;
						font-size: 14px;
						color: #999;
					}
				}
				&.search{
					padding-left: 0;
					.navbar-content__search-icons{
						margin-left: 10px;
						margin-right: 10px;
					}
					.navbar-search{
						border-radius: 5px;
					}
				}
			}
		}
	}
</style>
